package j78;

import cn.hutool.core.thread.ThreadUtil;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * @author chengshuai
 * @date 2021/4/18 16:03
 */
public class Client78 {

    public static void main(String[] args) {

        Object o1 = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue();
        WeakReference<Object> weakReference = new WeakReference<>(o1, referenceQueue);

        System.out.println(o1);
        System.out.println(weakReference.get());
        System.out.println("--");
        System.out.println(referenceQueue.poll());

        System.out.println("========");

        o1 = null;
        System.gc();
        ThreadUtil.sleep(500);

        System.out.println(o1);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
    }
}
